package com.freshcity.component.alert
{
	import com.freshcity.graphics.InfoBackground;
	
	import fl.controls.Button;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class GAlertWin extends Sprite
	{
		protected var title:DisplayObject;
		
		protected var message:TextField;
		
		protected var background:DisplayObject;
		
		protected var prompt:DisplayObjectContainer;
		
		protected var boarder:DisplayObject;
		
		protected var icon:DisplayObject;
		
		protected var boarderWidth:int=15;
		
		protected var minWidth:int=207;
		
		protected var minHeight:int=86;
		
		protected var stageWidth:Number;
		
		protected var stageHeight:Number;
		
		protected var closeHandler:Function;
		
		protected var closeBtn:Button;
		
		protected var extendObj:Object;
		
		protected var allButtons:Vector.<Button>;
		
		protected var _promptBackground:Sprite;
		
		public function GAlertWin(width:Number, height:Number)
		{
			this.allButtons = new Vector.<Button>();
			this.stageWidth = width;
			this.stageHeight = height;
			this.createChildren();
			this.childrenCreated();
			this.addDragHandleer();
			this.closeHandler = Alert.closeHandler;
			this.extendObj = Alert.extendObj;
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
		}
		
		protected function createBackground():DisplayObject
		{
			var loc2:*=0;
			var loc3:*=null;
			var loc4:*=null;
			var loc5:*=null;
			var loc6:*=null;
			var loc7:*=0;
			var loc8:*=null;
			var loc1:*=new Sprite();
			loc2 = Alert.backgroundColor;
			var loc9:*=Alert.mode;
			switch (loc9) 
			{
				case Alert.Mode_Blur:
				{
					loc3 = new BitmapData(this.stageWidth, this.stageHeight, true, 4278190080 + loc2);
					(loc4 = new BitmapData(this.stageWidth, this.stageHeight)).draw(stage);
					loc5 = new Rectangle(0, 0, this.stageWidth, this.stageHeight);
					loc6 = new Point(0, 0);
					loc7 = 120;
					loc3.merge(loc4, loc5, loc6, loc7, loc7, loc7, loc7);
					loc3.applyFilter(loc3, loc5, loc6, new BlurFilter(5, 5));
					loc8 = new Bitmap(loc3);
					loc1.addChild(loc8);
					loc1.mouseEnabled = true;
					break;
				}
				case Alert.Mode_None:
				{
					loc1.graphics.beginFill(loc2, 0);
					loc1.graphics.drawRect(0, 0, this.stageWidth, this.stageHeight);
					loc1.graphics.endFill();
					loc1.mouseEnabled = true;
					break;
				}
				case Alert.Mode_NoneNotModal:
				{
					loc1.graphics.beginFill(loc2, 0);
					loc1.graphics.drawRect(0, 0, this.stageWidth, this.stageHeight);
					loc1.graphics.endFill();
					loc1.mouseEnabled = false;
					break;
				}
				case Alert.Mode_Simple:
				{
					loc1.graphics.beginFill(loc2, Alert.backgroundAlpha);
					loc1.graphics.drawRect(0, 0, this.stageWidth, this.stageHeight);
					loc1.graphics.endFill();
					loc1.mouseEnabled = true;
					break;
				}
			}
			return loc1;
		}
		
		protected function doStopDrag(arg1:MouseEvent):void
		{
			if (arg1.target is Sprite) 
			{
				arg1.currentTarget.stopDrag();
			}
			return;
		}
		
		protected function getBlurFilter():BitmapFilter
		{
			var loc1:*=100;
			var loc2:*=100;
			return new BlurFilter(loc1, loc2, BitmapFilterQuality.HIGH);
		}
		
		protected function getDropShadowFilter():DropShadowFilter
		{
			var loc1:*=0;
			var loc2:*=90;
			var loc3:*=0.6;
			var loc4:*=12;
			var loc5:*=4;
			var loc6:*=1;
			var loc7:*=1;
			var loc8:*=false;
			var loc9:*=false;
			var loc10:*=BitmapFilterQuality.LOW;
			return new DropShadowFilter(loc6, loc2, loc1, loc3, loc4, loc5, loc7, loc10, loc8, loc9);
		}
		
		protected function removeDragHandleer():void
		{
			this.prompt.removeEventListener(MouseEvent.MOUSE_DOWN, doStartDrag);
			this.prompt.removeEventListener(MouseEvent.MOUSE_UP, this.doStopDrag);
			return;
		}
		
		protected function getGlowFilter():GlowFilter
		{
			var loc1:*=16777215;
			var loc2:*=0.8;
			var loc3:*=15;
			var loc4:*=15;
			var loc5:*=0.7;
			var loc6:*=true;
			var loc7:*=false;
			var loc8:*=BitmapFilterQuality.HIGH;
			return new GlowFilter(loc1, loc2, loc3, loc4, loc5, loc8, loc6, loc7);
		}
		
		protected function RGBToHex(arg1:int, arg2:int, arg3:int):int
		{
			var loc1:*=0;
			return loc1 = (loc1 = (loc1 = arg1 & 255 << 16) + (arg2 & 255 << 8)) + (arg3 & 255);
		}
		
		protected function keepInBounds(arg1:int):int
		{
			if (arg1 < 0) 
			{
				arg1 = 0;
			}
			if (arg1 > 255) 
			{
				arg1 = 255;
			}
			return arg1;
		}
		
		protected function buttonClickHandler(arg1:MouseEvent):void
		{
			var loc1:*=0;
			while (loc1 < this.allButtons.length) 
			{
				this.allButtons[loc1].removeEventListener(MouseEvent.CLICK, this.buttonClickHandler);
				++loc1;
			}
			this.removeDragHandleer();
			this.parent.removeChild(this);
			if (this.closeHandler != null) 
			{
				if (this.extendObj == null) 
				{
					this.closeHandler(Number(arg1.currentTarget.name.substr(4)));
				}
				else 
				{
					this.closeHandler(Number(arg1.currentTarget.name.substr(4)), this.extendObj);
				}
			}
			return;
		}
		
		protected function onResize(arg1:Event):void
		{
			if (this.background) 
			{
				if (this.stage) 
				{
					this.background.width = this.stage.stageWidth;
					this.background.height = this.stage.stageHeight;
				}
			}
			return;
		}
		
		public function get promptBackground():DisplayObject
		{
			return this._promptBackground;
		}
		
		public function getButtonByFlag(arg1:int):Button
		{
			var loc1:*=null;
			var loc2:*=0;
			var loc3:*=this.allButtons;
			for each (loc1 in loc3) 
			{
				if (loc1.name != "btn_" + arg1) 
				{
					continue;
				}
				return loc1;
			}
			return null;
		}
		
		protected static function doStartDrag(arg1:MouseEvent):void
		{
			if (arg1.target is Sprite && !(arg1.target is Button)) 
			{
				arg1.currentTarget.startDrag();
			}
			return;
		}
		
		protected static function ByteToArray(arg1:uint):Array
		{
			var loc1:*=new Array();
			loc1.push(arg1 & 1);
			loc1.push((arg1 & 2) >> 1);
			loc1.push((arg1 & 4) >> 2);
			loc1.push((arg1 & 8) >> 3);
			return loc1;
		}
		
		protected static function hexToRGB(arg1:uint):Array
		{
			var loc1:*=new Array();
			loc1.push(arg1 >> 16);
			var loc2:*=arg1 ^ loc1[0] << 16;
			loc1.push(loc2 >> 8);
			loc1.push(loc2 ^ loc1[1] << 8);
			return loc1;
		}
		
		internal function onAddToStage(arg1:Event):void
		{
			this.stage.addEventListener(Event.RESIZE, this.onResize);
			return;
		}
		
		protected function onCloseBtnClickHandler(arg1:MouseEvent):void
		{
			var loc1:*=0;
			while (loc1 < this.allButtons.length) 
			{
				this.allButtons[loc1].removeEventListener(MouseEvent.CLICK, this.buttonClickHandler);
				++loc1;
			}
			if (this.closeBtn != null) 
			{
				this.closeBtn.removeEventListener(MouseEvent.CLICK, this.onCloseBtnClickHandler);
			}
			this.removeDragHandleer();
			this.parent.removeChild(this);
			return;
		}
		
		protected function createChildren():void
		{
			if (Alert.title is DisplayObject) 
			{
				this.title = Alert.title;
			}
			else 
			{
				this.title = this.createTextField();
				if (Alert.titleTextFormat) 
				{
					(this.title as TextField).defaultTextFormat = Alert.titleTextFormat;
				}
			}
			this.message = this.createTextField();
			if (!Alert.messageTextFormat) 
			{
			};
			this.background = this.createBackground();
			this.boarder = this.createPromptBoarder();
			this.allButtons = this.createButtons();
			if (Alert.iconClass) 
			{
				this.icon = new Alert.iconClass();
			}
			if (Alert.showCloseBtn) 
			{
				this.closeBtn = new Button();
				//this.closeBtn.styleName = "CloseButton";
				this.closeBtn.setSize(19, 15);
				this.closeBtn.label = "";
				this.closeBtn.addEventListener(MouseEvent.CLICK, this.onCloseBtnClickHandler);
				this._promptBackground.addChild(this.closeBtn);
			}
			return;
		}
		
		protected function childrenCreated():void
		{
			var loc8:*=null;
			var loc1:TextField=this.title as TextField;
			loc1.htmlText = Alert.title;
			this.message.htmlText = Alert.text;
			var loc2:*=new Sprite();
			var loc3:*=0;
			while (loc3 < this.allButtons.length) 
			{
				(loc8 = this.allButtons[loc3]).addEventListener(MouseEvent.CLICK, this.buttonClickHandler);
				loc2.addChild(loc8);
				loc8.x = loc3 * (Alert.buttonWidth + 15);
				++loc3;
			}
			var loc4:*;
			(loc4 = new Sprite()).addChild(this.message);
			if (this.icon) 
			{
				loc4.addChild(this.icon);
				this.message.x = this.icon.width + 15;
			}
			var loc5:*=this.boarderWidth * 2;
			var loc6:*=Math.max(this.title.width + loc5, this.message.width + loc5, loc2.width + loc5, this.minWidth);
			var loc7:*=Math.max(this.title.height + 5, 15) + this.message.height + Alert.buttonHeight + 30;
			
			loc1.autoSize = TextFieldAutoSize.NONE;
			loc1.width = loc6 - 10;
			loc1.x = 5;
			loc1.y = 5;
			this.message.x = loc6 / 2 - this.message.width / 2;
			this.message.y = Math.max(this.title.height + 15, 15);
			loc2.x = (loc6 - (Alert.buttonWidth + 15) * this.allButtons.length + 15) / 2;
			loc2.y = this.message.y + this.message.height + 15;
			this.boarder.width = loc6;
			this.boarder.height = loc7;
			this.prompt = new Sprite();
			addChild(this.background);
			this.prompt.addChild(this.boarder);
			this.prompt.addChild(this.title);
			this.prompt.addChild(this.message);
			if (this.icon) 
			{
				addChild(this.icon);
			}
			this.prompt.addChild(loc2);
			addChild(this.prompt);
			this.prompt.x = (this.stageWidth - this.prompt.width) / 2;
			this.prompt.y = (this.stageHeight - this.prompt.height) / 2;
			if (Alert.showCloseBtn) 
			{
				this.closeBtn.x = loc6 - this.closeBtn.width - 8;
				this.closeBtn.y = 7;
			}
		}
		
		protected function addDragHandleer():void
		{
			this.prompt.addEventListener(MouseEvent.MOUSE_DOWN, doStartDrag);
			this.prompt.addEventListener(MouseEvent.MOUSE_UP, this.doStopDrag);
		}
		
		public function get window():DisplayObjectContainer
		{
			return this.prompt;
		}
		
		protected function createPromptBoarder():DisplayObject
		{
			this._promptBackground = new InfoBackground();
			this._promptBackground.filters = [this.getDropShadowFilter()];
			//this._promptBackground.filters = [this.getGlowFilter(), this.getDropShadowFilter()];
			//this._promptBackground.alpha = Alert.backgroundAlpha;
			return this._promptBackground;
		}
		
		protected function createButtons():Vector.<Button>
		{
			var loc3:*=null;
			var loc4:*=null;
			var loc5:*=null;
			var loc6:*=null;
			var loc1:*=new Vector.<Button>();
			var loc2:*=this.getButtonFlagsList();
			if (loc2[0] == 1) 
			{
				loc3 = new Button();
				loc3.label = Alert.yesLabel;
				loc3.name = "btn_" + Alert.YES;
				loc3.setSize(Alert.buttonWidth, Alert.buttonHeight);
				if (Alert.buttonStyleName) 
				{
					loc3.styleName = Alert.buttonStyleName;
				}
				loc1.push(loc3);
			}
			if (loc2[1] == 1) 
			{
				(loc4 = new Button()).label = Alert.noLabel;
				loc4.name = "btn_" + Alert.NO;
				loc4.setSize(Alert.buttonWidth, Alert.buttonHeight);
				if (Alert.buttonStyleName) 
				{
					loc4.styleName = Alert.buttonStyleName;
				}
				loc1.push(loc4);
			}
			if (loc2[2] == 1) 
			{
				(loc5 = new Button()).label = Alert.okLabel;
				loc5.name = "btn_" + Alert.OK;
				loc5.setSize(Alert.buttonWidth, Alert.buttonHeight);
				if (Alert.buttonStyleName) 
				{
					loc5.styleName = Alert.buttonStyleName;
				}
				loc1.push(loc5);
			}
			if (loc2[3] == 1) 
			{
				(loc6 = new Button()).label = Alert.calcelLabel;
				loc6.name = "btn_" + Alert.CANCEL;
				loc6.setSize(Alert.buttonWidth, Alert.buttonHeight);
				if (Alert.buttonStyleName) 
				{
					loc6.styleName = Alert.buttonStyleName;
				}
				loc1.push(loc6);
			}
			return loc1;
		}
		
		protected function getButtonFlagsList():Array
		{
			var loc1:Array=null;
			if (Alert.buttonFlags == 0) 
			{
				loc1 = ByteToArray(Alert.defaultButtonFlag);
			}
			else 
			{
				loc1 = ByteToArray(Alert.buttonFlags);
			}
			return loc1;
		}
		
		protected function createTextField():TextField
		{
			var loc1:TextField = new TextField();
			var fmt:TextFormat = new TextFormat("黑体",12,Alert.textColor);
			fmt.indent = 10;
			fmt.leading = 4;
			loc1.defaultTextFormat = fmt;
			loc1.multiline = true;
			loc1.wordWrap = true;
			loc1.selectable = false;
			loc1.mouseEnabled = false;
			loc1.width = 210;
			loc1.autoSize = TextFieldAutoSize.LEFT;
			//loc1.filters = [new GlowFilter(0, 1, 2, 2, 2)];
			return loc1;
		}
		
	}
}